package lab03.test03;


import java.io.*;
/**
 * Created by tianxiadiyishuai on 2017/12/13.
 */
public class Test {
    public static void main(String[] args) {
        String path = "D:\\test.txt";
        //字节流写文件
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(path);
            fileOutputStream.write("北京欢迎你！".getBytes());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            FileInputStream fileInputStream = null;
            try {
                fileInputStream = new FileInputStream(path);
                int tmp;
                while ((tmp = fileInputStream.read()) != -1) {
                    System.out.println(((char)tmp));
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        BufferedWriter writer = null;
        BufferedReader reader = null;
        try {
            writer=new BufferedWriter(new FileWriter(path));
            writer.write("我的学号是：你自己的学号");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            String line=null;
            reader = new BufferedReader(new FileReader(path));
            while ((line=reader.readLine())!=null)
            {
                System.out.println(line);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
